package customerController

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"go-demo-2025/controllers"
	"go-demo-2025/models/model"
	"go-demo-2025/service/users"
	"go-demo-2025/utils/funcUtils"
	"go-demo-2025/validator/customerValidator"
	"strconv"
)

// 获取客户列表
func GetCustomerList(c *gin.Context) {
	//入参校验
	var requestData users.UserListRequest
	err := c.Bind(&requestData) //执行绑定
	//fmt.Println("获取客户端请求的参数：", requestData)
	if err != nil {
		controllers.ReturnError(c, 1001, fmt.Sprintf("请求参数错误: %s", err))
		return
	}

	//调用service查询数据
	service := users.NewUserService()
	dataList, count := service.QueryUserList(requestData)

	//自定义要返回的字段
	showFields := []string{"id", "user_id", "name", "name_pinyin", "age", "age_desc", "email", "address", "status", "status_desc"}
	var resultList []map[string]any
	for _, item := range dataList {
		fmt.Println("item:", item)
		itemMap := funcUtils.ConvertToFlatMap(item, "") // 通过反射将嵌套结构体转换为一维 map
		fmt.Println("itemMap:", itemMap)
		fmt.Println()

		itemData := make(map[string]any)
		for _, field := range showFields {
			itemData[field] = itemMap[field]
		}
		resultList = append(resultList, itemData)
	}

	controllers.ReturnSuccess(c, 200, "success", resultList, int(count))
}

func GetCustomerInfo(c *gin.Context) {
	paramId := c.Param("id")
	if paramId == "" {
		controllers.ReturnError(c, 1001, fmt.Sprintf("请求参数错误: 缺少id"))
		return
	}

	id, _ := strconv.Atoi(paramId)

	//调用service查询数据
	service := users.NewUserService()
	dataDetail := service.QueryUserDetail(int64(id))
	//fmt.Println(dataDetail)

	controllers.ReturnSuccess(c, 200, "success", dataDetail, 1)
}

// 添加客户数据
func AddCustomer(c *gin.Context) {
	requestDataMapObj, _ := c.Get("requestDataMap") //接收上一步Set的数据(map)
	if requestDataMapObj == nil {
		fmt.Println("AddCustomer.requestDataMapObj is nil")
		return
	}
	requestDataMap := requestDataMapObj.(map[string]string)
	fmt.Println("AddCustomer.requestDataMap", requestDataMap)

	requestData, _ := c.Get("requestData") //接收上一步Set的数据(结构体)
	fmt.Println("AddCustomer.requestData", requestData)
	if requestData == nil {
		fmt.Println("AddCustomer.requestData is nil")
		return
	}

	//绑定需要入库的字段
	var insertData model.User
	if err := c.ShouldBindBodyWithJSON(&insertData); err != nil {
		errMsg := fmt.Sprintf("model参数绑定错误: %v", err)
		fmt.Println(errMsg)
		controllers.ReturnError(c, 1001, errMsg)
		return
	}
	fmt.Println("insertData:", insertData)

	// 通过 map 方式访问 requestDataMap 的 extra_data 字段(不推荐,会把原参数的json变为只有value没有key的字符串数据)
	//extraData, _ := json.Marshal(requestDataMap["extra_data"])
	//insertData.Extra = string(extraData)

	// 通过 结构体 方式访问 requestData.ExtraData 字段(推荐,会保持原参数的json数据格式的字符串)
	extraData, _ := json.Marshal(requestData.(customerValidator.ValidateData).ExtraData)
	insertData.Extra = string(extraData)

	//调用service层的逻辑添加数据
	service := users.NewUserService()
	res, err := service.AddCustomer(insertData)
	if err != nil {
		controllers.ReturnError(c, 400, fmt.Sprintf("添加失败:%v", err))
		return
	}

	controllers.ReturnSuccess(c, 200, "添加成功", res, 1)
}
